import 'dart:math';

import 'package:first_flutter_project/main/main.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

import '../AfterLayoutRoute.dart';
import '../Gesture/GestureDetectorConflictTest.dart';
import '../Gesture/GestureDetectorHitTest.dart';
import '../Gesture/NotificationInterruptTest.dart';

///组合动画用法
///
///抽奖滚动动画
///

class RollingLotteryAnimator extends StatefulWidget {
  @override
  _RollingLotteryAnimatorState createState() => _RollingLotteryAnimatorState();
}

class _RollingLotteryAnimatorState extends State<RollingLotteryAnimator>
    with TickerProviderStateMixin {
  late AnimationController _controller;
  late Animation animation;
  ScrollController _scrollController = new ScrollController();

  int currentScrollIndex = 0;
  double currentDuring = 1;
  double lastScroll = 0;

  //手指向上滑，view向下滚
  bool scrollDown = true;


  //stack布局
  // int stackCurrentScrollIndex = 0;
  // double stackCurrentDuring = 1;
  // double stackLastScroll = 0;


  @override
  void initState() {
    super.initState();

    // _controller =
    //     AnimationController(duration: Duration(seconds: 4), vsync: this);
    // animation = CurvedAnimation(
    //     parent: _controller, curve: Cubic(0.25, 0.5, 0.75, 1.0));
    //
    // _controller.forward();
    // animation.addListener(() => setState(() {}));

    // _controller.addStatusListener((status) {
    //   if (status == AnimationStatus.completed)
    //     _controller.reverse(from: 400);
    //   else if (status == AnimationStatus.dismissed) _controller.forward();
    // });

    WidgetsBinding.instance?.addPostFrameCallback((_) {
      lastScroll = 25;
      _scrollController.jumpTo(25);
    });

    _scrollController.addListener(() {
      print("滚动${_scrollController.offset}");
      if (_scrollController.offset > lastScroll) {
        scrollDown = true;
      } else {
        scrollDown = false;
      }

      print("滚动进度：${_scrollController.offset % 50 / 50}");
      currentDuring = _scrollController.offset % 50 / 50;
      currentScrollIndex = _scrollController.offset ~/ 50;
      print("滚动号：$currentScrollIndex");
      setState(() {});
    });


  }

  double next(int min, int max) {
    double res = min + Random().nextInt(max - min).toDouble();
    print("tgw滚动指定到：$res");
    return res;
  }

  void onPressed() {
    var scrollPath = next(0, 30 * 50 - 100);
    if (scrollPath < 25) {
      scrollPath = 25;
    } else if (scrollPath >= 25) {
      var ban = scrollPath % 25;
      if (ban <= 12.5) {
        scrollPath = scrollPath - ban;
        if (scrollPath % 50 == 0) {
          scrollPath = scrollPath + 25;
        }
      } else {
        scrollPath = scrollPath - ban + 25;
        if (scrollPath % 50 == 0) {
          scrollPath = scrollPath + 25;
        }
      }
    }
    print("tgw滚动指定到最终：$scrollPath");
    _scrollController.animateTo(scrollPath,
        duration: Duration(seconds: 5), curve: Cubic(0.8, 0.8, 0.8, 1));
  }

  _playAnimation() async {
    try {
      //先正向执行动画
      await _controller.forward().orCancel;
      //再反向执行动画
      await _controller.reverse().orCancel;
    } on TickerCanceled {
      // the animation got canceled, probably because we were disposed
    }
  }

  @override
  Widget build(BuildContext context) {
    return Material(
      child: Center(
        child: Container(
          child: Column(
            children: [
              Stack(
                children: [
                  Container(
                    child: Image.network(
                      "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fi-1.lanrentuku.com%2F2020%2F12%2F3%2F7e21f352-c331-432b-9d14-19550fdcdb84.jpg%3FimageView2%2F2%2Fw%2F500&refer=http%3A%2F%2Fi-1.lanrentuku.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=auto?sec=1656071301&t=8e6bfe07e4e9d5a7d5eb922b97995dec",
                      width: 300,
                      fit: BoxFit.fill,
                      height: 100,
                    ),
                    width: 300,
                    height: 100,
                    // color: Color(0xFF0AD5B8),
                  ),

                  Container(
                    child: SingleChildScrollView(
                        controller: _scrollController,
                        // physics: new AlwaysScrollableScrollPhysics(),
                        // padding: EdgeInsets.only(top: 600,bottom: 600),
                        child: Column(
                          children: initWidgets(),
                        )),
                        // child: Stack(
                        //   children: initWidgets1(),
                        // )),
                    width: 300,
                    height: 100,
                    // color: Color(0xFF0AD5B8),
                  ),
                  // Container(
                  //   width: 300,
                  //   height: 100,
                  //   color: Color(0xffffff),
                  // ),
                ],
              ),
              TextButton(onPressed: onPressed, child: Text("抽奖"))
            ],
          ),
          width: 300,
          height: 500,
          color: Color(0x2CD21E1E),
        ),
      ),
    );
  }

  bool isSelected(int currentIndex, int currentScrollIndex) {
    return currentIndex == (currentScrollIndex + 1) ? true : false;
  }

  double checkTextSize(int currentIndex, int currentScrollIndex) {
    if (isSelected(currentIndex, currentScrollIndex)) {
      return 30;
    } else {
      return 20;
    }
  }

  List<Widget> initWidgets() {
    List<Widget> list = <Widget>[];
    for (int i = 0; i < 30; i++) {
      list.add(
        Container(
          child: Center(
            child: isSelected(i, currentScrollIndex)
                ? Center(
                    child: Text(
                      '落叶的位置，谱出$i',
                      style: TextStyle(
                          fontSize: (24 + 6 * (1 - currentDuring)),
                          color: Color.fromARGB(
                              (255 *
                                      (currentDuring <= 0.5
                                          ? (0.5 + currentDuring)
                                          : 80))
                                  .toInt(),
                              255,
                              255,
                              255)),
                    ),
                  )
                // : ShaderMask(
                //     shaderCallback: (Rect bounds) {
                //       return LinearGradient(
                //         begin: Alignment.topCenter,
                //         end: Alignment.bottomCenter,
                //         colors: <Color>[Colors.red, Color(0xA6FFFFFF)],
                //         tileMode: TileMode.mirror,
                //       ).createShader(bounds);
                //     },
                //     blendMode: BlendMode.srcATop,
                //     child: Center(
                //       child: Text(
                //         '落叶的位置，谱出一首诗。',
                //         style: TextStyle(
                //             fontSize:
                //                 scrollDown? i == (currentScrollIndex + 2)?20 + 4 * (currentDuring):20:20
                //
                //         ),
                //             // i == (currentScrollIndex)
                //             //     ? 24 - 4 * (1 - currentDuring)
                //             //     : i == (currentScrollIndex + 2)
                //             //         ? 20 + 4 * (currentDuring)
                //             //         : 24),
                //       ),
                //     ),
                //   ),
                : Center(
                    child: Text(
                      '落叶的位置，谱出一$i。',
                      style: TextStyle(
                          fontSize: scrollDown
                              ? i == (currentScrollIndex + 2)
                                  ? 20 + 4 * (currentDuring)
                                  : 20 - 4 * (1 - currentDuring)
                              : 20,
                          color: i == (currentScrollIndex + 2)
                              ? Color.fromARGB(
                                  (currentDuring <= 0.5
                                          ? 80
                                          : 255 * (currentDuring - 0.5))
                                      .toInt(),
                                  255,
                                  255,
                                  255)
                              : Color.fromARGB((80).toInt(), 255, 255, 255)),
                    ),
                    // i == (currentScrollIndex)
                    //     ? 24 - 4 * (1 - currentDuring)
                    //     : i == (currentScrollIndex + 2)
                    //         ? 20 + 4 * (currentDuring)
                    //         : 24),
                  ),
          ),
          height: 50,
        ),
      );
    }
    return list;
  }

  List<Widget> initWidgets1() {
    List<Widget> list = <Widget>[];
    for (int i = 0; i < 30; i++) {
      list.add(
        Container(
          margin: EdgeInsets.only(top: (30 * i).toDouble()),
          child: Center(
            child: isSelected(i, currentScrollIndex)
                ? Center(
                    child: Text(
                      '落叶的位置，谱出$i',
                      style: TextStyle(
                          fontSize: (24 + 6 * (1 - currentDuring)),
                          color: Color.fromARGB(
                              (255 *
                                      (currentDuring <= 0.5
                                          ? (0.5 + currentDuring)
                                          : 80))
                                  .toInt(),
                              255,
                              255,
                              255)),
                    ),
                  )
                : Center(
                    child: Text(
                      '落叶的位置，谱出一$i。',
                      style: TextStyle(
                          fontSize: scrollDown
                              ? i == (currentScrollIndex + 2)
                                  ? 20 + 4 * (currentDuring)
                                  : 20 - 4 * (1 - currentDuring)
                              : 20,
                          color: i == (currentScrollIndex + 2)
                              ? Color.fromARGB(
                                  (currentDuring <= 0.5
                                          ? 80
                                          : 255 * (currentDuring - 0.5))
                                      .toInt(),
                                  255,
                                  255,
                                  255)
                              : Color.fromARGB((80).toInt(), 255, 255, 255)),
                    ),
                  ),
          ),
          height: 50,
        ),
      );
    }
    return list;
  }
}

class ClipperPath extends CustomClipper<Rect> {
  @override
  Rect getClip(Size size) {
    return new Rect.fromLTRB(0, 10, size.width, size.height);
  }

  @override
  bool shouldReclip(CustomClipper<Rect> oldClipper) {
    return true;
  }

// child: Column(
//   children: [
//     Container(
//         child: ClipRect(
//           child: Center(
//             child: ShaderMask(
//               shaderCallback: (Rect bounds) {
//                 return LinearGradient(
//                   begin: Alignment.topCenter,
//                   end: Alignment.bottomCenter,
//                   colors: <Color>[
//                     Color(0x27FFFFFF),
//                     Color(0xA6FFFFFF)
//                   ],
//                   tileMode: TileMode.mirror,
//                 ).createShader(bounds);
//               },
//               blendMode: BlendMode.srcATop,
//               child: Center(
//                 child: Text(
//                   '落叶的位置，谱出一首诗。',
//                   style: TextStyle(fontSize: 20),
//                 ),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: Center(
//             child: Text(
//               '落叶的位置。落叶',
//               style:
//                   TextStyle(fontSize: 30, color: Color(0xFFFFFFFF)),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//     Container(
//         child: Center(
//           child: ShaderMask(
//             shaderCallback: (Rect bounds) {
//               return LinearGradient(
//                 begin: Alignment.topCenter,
//                 end: Alignment.bottomCenter,
//                 colors: <Color>[
//                   Color(0x27FFFFFF),
//                   Color(0xA6FFFFFF)
//                 ],
//                 tileMode: TileMode.mirror,
//               ).createShader(bounds);
//             },
//             blendMode: BlendMode.srcATop,
//             child: Center(
//               child: Text(
//                 '落叶的位置，谱出一首诗。',
//                 style: TextStyle(fontSize: 20),
//               ),
//             ),
//           ),
//         ),
//         height: 50,
//         color: Color(0xFF088AEF)),
//   ],
// ),
}
